<?php

/**
 * @author ducnhat
 * @email ddnhat@gmail.com
 * @copyright 2014
 */
 
class Admin_SettingController extends Zenddn_Controller_Action{
	
	public function init(){
		parent::init();
        $this->loadTemplate('alpona');
        $this->view->headTitle = "setting";
	}
	
	public function indexAction(){
	    $baseUrl = $this->view->baseUrl();
        
        parent::addCustomeScript($baseUrl . '/fancybox/jquery.fancybox.js');
        parent::addCustomeStyleSheet($baseUrl . '/fancybox/jquery.fancybox.css?v=2.1.5');
        
		$request = $this->_request;
        
        $model = new Admin_Model_Setting();
        
        if($model->checkExists('page')){
            $page = $model->getData('page');
            $this->view->page = unserialize($page['value']);
        }
        
        if($model->checkExists('webinfo')){
            $webinfo = $model->getData('webinfo');
            $this->view->webinfo = unserialize($webinfo['value']);
        }
        
        if($model->checkExists('mail_server')){
            $mail_server = $model->getData('mail_server');
            $this->view->mail_server = unserialize($mail_server['value']);
        }
        
        if($model->checkExists('social')){
            $social = $model->getData('social');
            $this->view->social = unserialize($social['value']);
        }
        
        if($model->checkExists('mailchimp')){
            $mailchimp = $model->getData('mailchimp');
            $this->view->mailchimp = unserialize($mailchimp['value']);
        }
        
        if($request->isPost()){
            $data = $request->getParams();
            unset($data['module']);
            unset($data['controller']);
            unset($data['action']);
        	
            foreach($data as $key => $value){
                $setting['key'] = $key;
                $setting['value'] = serialize($value);
                
                $model->saveData($setting);
            }
            
            $this->_helper->_redirector->gotoRoute(array('module' => 'admin', 'controller' => 'setting', 'action' => 'index'), null, true);
        }
	}
    
    public function pageAction(){
    	$request = $this->_request;
        
        $taxonomy_model = new Admin_Model_Taxonomy();
        $categories = $taxonomy_model->getAllData('post');
        
        $model = new Admin_Model_Setting();
        if($model->checkExists("page_category")){
            $setting = $model->getData("page_category");
            
            $this->view->data = unserialize($setting['value']);
        }
        
        
        if($request->isPost()){
            $param = $request->getParams();
            unset($param['module']);
            unset($param['controller']);
            unset($param['action']);
            
            $data['key'] = "page_category";
            $data['value'] = serialize($param);
            
            $model->saveData($data);
            
            $this->_helper->_redirector->gotoRoute(array('module' => 'admin', 'controller' => 'setting', 'action' => 'page'), null, true);
        }
        
        $this->view->categories = $categories;  
    }
}